package com.ithuangyl.common.config;

import android.content.Context;
import android.os.Handler;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Interceptor;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/7/11 21:24
 */
public class Configurator {

    private static final HashMap<Object, Object> LATTE_CONFIGS = new HashMap<>();
    private static final ArrayList<Interceptor> INTERCEPTORS = new ArrayList<>();
    private static final Handler HANDLER= new Handler();

    private Configurator() {
        LATTE_CONFIGS.put(ConfigKeys.CONFIG_READY, false);
        LATTE_CONFIGS.put(ConfigKeys.HANDLER,HANDLER);
    }

    private static class ConfiguratorHolder {
        public static final Configurator CONFIGURATOR = new Configurator();
    }

    public static Configurator get() {
        return ConfiguratorHolder.CONFIGURATOR;
    }

    private static void checkConfiguration() {
        final boolean isReady = (boolean) LATTE_CONFIGS.get(ConfigKeys.CONFIG_READY);
        if (!isReady) {
            throw new RuntimeException("Configuration is not ready,call configure");
        }
    }
    /**
     * 在初始化完成之前，用到了是否为测试环境的这个参数，
     * 一旦调用了这个方法就默认初始化完成了，之后应尽快调用configure方法
     */
    public Configurator withDebug(boolean isDebug) {
        LATTE_CONFIGS.put(ConfigKeys.IS_DEBUG, isDebug);


        LATTE_CONFIGS.put(ConfigKeys.CONFIG_READY, true);
        return this;
    }

    public final Configurator withInterceptor(Interceptor interceptor) {
        if (interceptor != null) {
            INTERCEPTORS.add(interceptor);
        }
        LATTE_CONFIGS.put(ConfigKeys.INTERCEPTOR, INTERCEPTORS);
        return this;
    }

    public Configurator withApplicationContext(Context context) {
        LATTE_CONFIGS.put(ConfigKeys.APPLICATION_CONTEXT, context);
        return this;
    }

    public Configurator withApiHost(String apiHost) {
        LATTE_CONFIGS.put(ConfigKeys.RELEASE_API_HOST, apiHost);
        return this;
    }

    public Configurator withApiHost2(String apiHost) {
        LATTE_CONFIGS.put(ConfigKeys.RELEASE_API_HOST2, apiHost);
        return this;
    }

    public Configurator withDebugApiHost(String apiHost) {
        LATTE_CONFIGS.put(ConfigKeys.DEBUG_API_HOST, apiHost);
        return this;
    }

    public Configurator withDebugApiHost2(String apiHost) {
        LATTE_CONFIGS.put(ConfigKeys.DEBUG_API_HOST2, apiHost);
        return this;
    }
    public Configurator withLoaderDelayed(int delayed) {
        LATTE_CONFIGS.put(ConfigKeys.LOADER_DELAYED, delayed);
        return this;
    }

    public final void configure() {
        LATTE_CONFIGS.put(ConfigKeys.CONFIG_READY, true);
    }

    @SuppressWarnings("unchecked")
    public <T> T getConfiguration(Object key) {
        checkConfiguration();
        final Object value = LATTE_CONFIGS.get(key);
        if (value == null) {
            throw new NullPointerException(key.toString() + " IS NULL");
        }
        return (T) value;
    }
}
